package com.ccl.iot;import com.ccl.iot.privates.ListMap;
import com.ccl.iot.IOTFormulas.IOTStringFormula;

public class IOTFormulaArray {
	private int iCount = 0;
	private ListMap<String, IOTStringFormula> iFormulas = null;
	
	IOTFormulaArray(String aFormula) {
		if (aFormula != null){
			String tFormulas[] = aFormula.split(",");
			
			if (tFormulas != null && tFormulas.length > 0){
				iFormulas = new ListMap<String, IOTStringFormula>();
				
				for (int i = 0; i < tFormulas.length; i++){
					IOTStringFormula tFormula = new IOTStringFormula(new String(tFormulas[i]));
					
					if (tFormula.isValid()){
						if (iFormulas.add(tFormula.getName(), tFormula)){
							iCount++;
						}
					}
				}
			}
		}
	}
	
	public int getCount(){
		return iCount;
	}

	public IOTStringFormula getFormula(String aName){
		if (iCount > 0){
			return iFormulas.getValue(aName);
		}
		
		return null;
	}

	@Override
	public String toString() {
		if (iCount > 0){
			StringBuilder tFormulas = new StringBuilder();
			
			for (int i = 0; i < iCount; i++){
				if (i > 0){
					tFormulas.append(",");
				}
				
				tFormulas.append(iFormulas.getValueAt(i).toString());
			}
			
			return tFormulas.toString();
		}else{
			return "";
		}
	}
}
